package gateway

import (
	"cloud-export-iotkit-mqtt/pkg/iita"
	"sync"
)

type Gateway struct {

	// 网关 连接信息
	GatewayConfig GatewayConfig
	// 网关熟悉属性
	iita.Device

	subDeviceMap sync.Map
}

func (g *Gateway) Initialize(otherConfig ...interface{}) error {

	g.Device = iita.Device{ProductKey: g.GatewayConfig.ProductKey, DeviceName: g.GatewayConfig.DeviceName, DeviceId: g.GatewayConfig.DeviceId, Desc: "铱塔智联网关",
		Model: g.GatewayConfig.Model}

	g.subDeviceMap = sync.Map{}

	return nil
}

func (g *Gateway) AddSubDevice(device iita.Device) error {
	g.subDeviceMap.Store(device.DeviceId, device)
	return nil
}

func (g *Gateway) RemoveSubDevice(device iita.Device) error {

	g.subDeviceMap.Delete(device.DeviceId)
	return nil
}

func (g *Gateway) GetSubDeviceById(deviceId string) (iita.Device, bool) {
	value, ok := g.subDeviceMap.Load(deviceId)
	if ok {
		return value.(iita.Device), ok
	}
	return iita.Device{}, ok
}

func (g *Gateway) GetDeviceMap() map[string]iita.Device {
	deviceMap := make(map[string]iita.Device)
	g.subDeviceMap.Range(func(key, value interface{}) bool {
		deviceMap[key.(string)] = value.(iita.Device)
		return true
	})
	return deviceMap

}

func (g *Gateway) RegisterSelf() error {
	//TODO implement me
	panic("implement me")
}

func (g *Gateway) RegisterDevice(device iita.Device) error {
	//TODO implement me
	panic("implement me")
}

func (g *Gateway) DeRegisterDeviceByName(deviceName string) error {
	//TODO implement me
	panic("implement me")
}

func (g *Gateway) Run(args ...interface{}) {
	//TODO implement me

}

func (g *Gateway) Stop() error {
	//TODO implement me
	panic("implement me")
}
